/**
 * History:
 *     20030820 Diablo Lan  Add a Method:getOptions(String value,String defaultValue)
 *                          can set Default value
 *     20030821 Diablo Lan  Modify getOptions(String value,String defaultValue)
 *                          dispose same value
 *     20070728 Lee Shu Sun remove /n
 */

// Copyright (c) 2002 HTHK
package tss.common;

import java.util.*;
import java.sql.*;

import org.apache.log4j.Logger;

import com.ffpc.sql.*;
/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public abstract class Options2
{
  protected ArrayList options;
  private Logger logger = Logger.getLogger(Options2.class);
  public static class Option
  {
          private String value;
          private String description;

          public Option()
          {
              setValue(new String(""));
              setDescription(new String(""));
          }

          public Option(String value, String description)
          {
              setValue(value);
              setDescription(description);
          }

          public void setValue(String value)
          {
              this.value = value;
          }

          public String getValue()
          {
              return this.value;
          }

          public void setDescription(String description)
          {
              this.description = description;
          }

          public String getDescription()
          {
              return this.description;
          }

          public String getHtml(String matchValue)
          {
              boolean selected = matchValue.equals(this.getValue());
              StringBuffer sb = new StringBuffer("   <option value=\"").append(this.getValue()).append("\"").append(selected?" selected ":" ").append(" >")
                                                            .append(this.getDescription()).append("</option>\n");
              return sb.toString();
          }
  }
  /**
   * Constructor
   */
  public Options2()
  {
  }

  protected void load()
  {
	  ConnectionFactory cf = null;
      try
      {
          cf = new ConnectionFactory("fasp_tss");
          Connection con = cf.getConnection();
          this.options = createOptions(con);
          
      }
      catch (Exception e)
      {
          logger.error(e,e);
	  } finally {
		  
		  try {
          	if (cf != null)
          		cf.finalize();
          } catch (Exception e) {
              logger.error("", e);
          }
          
	  }
	  
  }

  public String getOptions(String value)
  {
      StringBuffer htmlOptions = new StringBuffer();
      Option nullOption = new Option("","");
      htmlOptions.append(nullOption.getHtml(value));
      for (int i=0; this.options!=null && i<this.options.size(); i++)
      {
          Option option = (Option)options.get(i);
          htmlOptions.append(option.getHtml(value));
      }
      return htmlOptions.toString();
  }

  /**
   * List All value and set a defaultValue to show
   * @param value                      All values
   * @param defaultValue               default value to show
   *
   * @return                           List Value and Description
   */
  public String getOptions(String value,String defaultValue)
  {
      StringBuffer htmlOptions = new StringBuffer();
      for (int i=0; this.options!=null && i<this.options.size(); i++)
      {

          Option option = (Option)options.get(i);
          if(!option.getDescription().equalsIgnoreCase(defaultValue))
          {
            htmlOptions.append(option.getHtml(value));
          }
          else
            htmlOptions.append("  <option value="+
                                    defaultValue+" selected>"+
                                    defaultValue +
/* CR1764 */
//                               "   </option>\n");
                               "   </option>");
/* CR1764 */
      }
      return htmlOptions.toString();

  }


  public String getOptions(String value, boolean readOnly)
  {
      if (!readOnly)
          return getOptions(value);
      StringBuffer htmlOptions = new StringBuffer();
      for (int i=0; this.options!=null && i<this.options.size(); i++)
      {
          Option option = (Option)options.get(i);
          if (option.getValue().equals(value))
            htmlOptions.append(option.getHtml(value));
      }
      return htmlOptions.toString();
  }

  protected abstract ArrayList createOptions(Connection con)
    throws Exception;
}

